home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / unixcpio.gz / unixnet.cpio / nr3.c < prev    next >
C/C++ Source or Header  |  1994-07-11  |  26KB  |  981 lines

  1. /* net/rom level 3 low level processing
  2.  * Copyright 1989 by Daniel M. Frank, W9NK.  Permission granted for
  3.  * non-commercial distribution only.
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include "global.h"
  8. #include "mbuf.h"
  9. #include "iface.h"
  10. #include "timer.h"
  11. #include "arp.h"
  12. #include "slip.h"
  13. #include "ax25.h"
  14. #include "netrom.h"
  15. #include "nr4.h"
  16. #include "lapb.h"
  17. #include <ctype.h>
  18. #ifdef    UNIX
  19. #include <string.h>
  20. #include <memory.h>
  21. #endif
  22.  
  23. /* Nodes message broadcast address: "NODES" in shifted ASCII */
  24. struct ax25_addr nr_nodebc = {
  25.     'N'<<1, 'O'<<1, 'D'<<1, 'E'<<1, 'S'<<1, ' '<<1,
  26.     ('0'<<1) | E
  27. } ;
  28.  
  29. struct nriface nrifaces[NRNUMIFACE] ;
  30. unsigned nr_numiface ;
  31. struct nrnbr_tab *nrnbr_tab[NRNUMCHAINS] ;
  32. struct nrroute_tab *nrroute_tab[NRNUMCHAINS] ;
  33. struct nrnf_tab *nrnf_tab[NRNUMCHAINS] ;
  34. unsigned nr_nfmode = NRNF_NOFILTER ;
  35. unsigned nr_ttl = 64 ;
  36. unsigned obso_init = 6 ;
  37. unsigned obso_minbc = 5 ;
  38. unsigned nr_maxroutes = 5 ;
  39. unsigned nr_autofloor = 1 ;
  40. unsigned nr_verbose = 0 ;
  41. struct interface *nr_interface ;
  42.  
  43. /* send a NET/ROM layer 3 datagram */
  44. void nr3output(dest, data)
  45. struct ax25_addr *dest ;
  46. struct mbuf *data ;
  47. {
  48.     struct nr3hdr n3hdr ;
  49.     struct mbuf *n3b ;
  50.  
  51.     n3hdr.dest = *dest ;    /* copy destination field */
  52.     n3hdr.ttl = nr_ttl ;    /* time to live from initializer parm */
  53.  
  54.     if ((n3b = htonnr3(&n3hdr)) == NULLBUF) {
  55.         free_p(data) ;
  56.         return ;
  57.     }
  58.  
  59.     append(&n3b, data) ;
  60.  
  61.     /* The null interface indicates that the packet needs to have */
  62.     /* an appropriate source address inserted by nr_route */
  63.     
  64.     nr_route(n3b,NULLAX25) ;
  65. }
  66.  
  67.  
  68. /* send IP datagrams across a net/rom network connection */
  69. /*ARGSUSED*/
  70. int
  71. nr_send(bp,interface,gateway,precedence,delay,throughput,reliability)
  72. struct mbuf *bp ;
  73. struct interface *interface ;
  74. int32 gateway ;
  75. char precedence ;
  76. char delay ;
  77. char throughput ;
  78. char reliability ;
  79. {
  80.     struct ax25_addr dest ;
  81.     struct mbuf *pbp ;
  82.     struct nr4hdr n4hdr ;
  83.     char *hwaddr ;
  84.     struct arp_tab *arp ;
  85.  
  86.     if ((arp = arp_lookup(ARP_NETROM,gateway)) == NULLARP) {
  87.         free_p(bp) ;    /* drop the packet if no route */
  88.         return ;
  89.     }
  90.     hwaddr = arp->hw_addr ;                /* points to destination */
  91.     memcpy(dest.call, hwaddr, ALEN) ;
  92.     dest.ssid = hwaddr[ALEN] ;
  93.         
  94.     /* Create a "network extension" transport header */
  95.     n4hdr.opcode = NR4OPPID ;
  96.     n4hdr.u.pid.family = PID_IP ;
  97.     n4hdr.u.pid.proto = PID_IP ;
  98.  
  99.     if ((pbp = htonnr4(&n4hdr)) == NULLBUF) {
  100.         free_p(bp) ;
  101.         return ;
  102.     }
  103.  
  104.     append(&pbp,bp) ;        /* Append the data to that */
  105.     nr3output(&dest, pbp) ; /* and pass off to level 3 code */
  106.  
  107. }
  108.  
  109. /* Figure out if a call is assigned to one of my net/rom
  110.  * interfaces.
  111.  */
  112. static int
  113. ismycall(addr)
  114. struct ax25_addr *addr ;
  115. {
  116.     register int i ;
  117.     int found = 0 ;
  118.     
  119.     for (i = 0 ; i < nr_numiface ; i++)
  120.         if (addreq((struct ax25_addr *)(nrifaces[i].interface->hwaddr),
  121.             addr)) {
  122.             found = 1 ;
  123.             break ;
  124.         }
  125.  
  126.     return found ;
  127. }
  128.  
  129.  
  130. /* Route net/rom network layer packets.
  131.  */
  132. nr_route(bp, iaxp)
  133. struct mbuf *bp ;            /* network packet */
  134. struct ax25_cb *iaxp ;        /* incoming ax25 control block */
  135. {
  136.     struct nr3hdr n3hdr ;
  137.     struct nr4hdr n4hdr ;
  138.     struct ax25_cb *axp, *find_ax25(), *open_ax25() ;
  139.     struct ax25 naxhdr ;
  140.     struct ax25_addr neighbor, from ;
  141.     struct mbuf *hbp, *pbp ;
  142.     extern int16 axwindow ;
  143.     void ax_incom(), nr4input() ;
  144.     register struct nrnbr_tab *np ;
  145.     register struct nrroute_tab *rp ;
  146.     register struct nr_bind *bindp ;
  147.     struct nr_bind *find_best() ;
  148.     struct interface *interface ;
  149.     unsigned ifnum ;
  150.     
  151.     if (ntohnr3(&n3hdr,&bp) == -1) {
  152.         free_p(bp) ;
  153.         return ;
  154.     }
  155.  
  156.     /* If this isn't an internally generated network packet,
  157.      * give the router a chance to record a route back to the
  158.      * sender, in case they aren't in the local node's routing
  159.      * table yet.
  160.      */
  161.  
  162.     if (iaxp != NULLAX25) {
  163.         /* find the interface number */
  164.         for (ifnum = 0 ; ifnum < nr_numiface ; ifnum++)
  165.             if (iaxp->interface == nrifaces[ifnum].interface)
  166.                 break ;
  167.  
  168.         if (ifnum == nr_numiface) {    /* shouldn't happen! */
  169.             free_p(bp) ;
  170.             return ;
  171.         }
  172.  
  173.         from = iaxp->addr.dest ;
  174.         from.ssid |= E ;
  175.  
  176.         /* Add (possibly) a zero-quality recorded route via */
  177.         /* the neighbor from which this packet was received */
  178.         /* Note that this doesn't work with digipeated neighbors, */
  179.         /* at this point. */
  180.         
  181.         (void) nr_routeadd("      ",&n3hdr.source,ifnum,0,
  182.                                     (char *)&from,0,1) ;
  183.     }
  184.  
  185.     /* A packet from me, to me, can only be one thing: */
  186.     /* a horrible routing loop.  This will probably result */
  187.     /* from a bad manual ARP entry, but we should fix these */
  188.     /* obscure errors as we find them. */
  189.     
  190.     if (ismycall(&n3hdr.dest)) {
  191.         if (iaxp == NULLAX25) {        /* From me? */
  192.             free_p(bp) ;
  193.             return ;
  194.         } else {                    /* It's from somewhere else! */
  195.             if (ntohnr4(&n4hdr,&bp) == -1) {
  196.                 free_p(bp) ;
  197.                 return ;
  198.             }
  199.             if ((n4hdr.opcode & NR4OPCODE) == 0) {
  200.                 if (n4hdr.u.pid.family == PID_IP
  201.                     && n4hdr.u.pid.proto == PID_IP)
  202.                     ip_route(bp,0) ;
  203.                 else                     /* we don't do this proto */
  204.                     free_p(bp) ;
  205.  
  206.                 return ;
  207.             }
  208.             
  209.             /* Must be net/rom transport: */
  210.  
  211.             nr4input(&n4hdr,bp) ;
  212.  
  213.         }
  214.         return ;
  215.     }
  216.  
  217.     if ((rp = find_nrroute(&n3hdr.dest)) == NULLNRRTAB) {
  218.         /* no route, drop the packet */
  219.         free_p(bp) ;
  220.         return ;
  221.     }
  222.  
  223.     if ((bindp = find_best(rp->routes,1)) == NULLNRBIND) {
  224.         /* This shouldn't happen yet, but might if we add */
  225.         /* dead route detection */
  226.         free_p(bp) ;
  227.         return ;
  228.     }
  229.  
  230.     np = bindp->via ;
  231.     memcpy(neighbor.call,np->call,ALEN) ;
  232.     neighbor.ssid = np->call[ALEN] ;
  233.     interface = nrifaces[np->interface].interface ;
  234.  
  235.     /* Now check to see if iaxp is null.  That is */
  236.     /* a signal that the packet originates here, */
  237.     /* so we need to insert the callsign of the appropriate  */
  238.     /* interface */
  239.     if (iaxp == NULLAX25)
  240.         memcpy((char *)&n3hdr.source,interface->hwaddr,AXALEN) ;
  241.     
  242.     /* Make sure there is a connection to the neighbor */
  243.     if ((axp = find_ax25(&neighbor)) == NULLAX25 ||
  244.         (axp->state != CONNECTED && axp->state != RECOVERY)) {
  245.         /* Open a new connection or reinitialize old one */
  246.         /* hwaddr has been advanced to point to neighbor + digis */
  247.         atohax25(&naxhdr, np->call, (struct ax25_addr *)interface->hwaddr) ;
  248.         axp = open_ax25(&naxhdr, axwindow, ax_incom, NULLVFP, NULLVFP,
  249.                         interface,(char *)0) ;
  250.         if (axp == NULLAX25) {
  251.             free_p(bp) ;
  252.             return ;
  253.         }
  254.     }
  255.         
  256.     if (--n3hdr.ttl == 0) {    /* the packet's time to live is over! */
  257.         free_p(bp) ;
  258.         return ;
  259.     }
  260.  
  261.     /* allocate and fill PID mbuf */
  262.     if ((pbp = alloc_mbuf(1)) == NULLBUF) {
  263.         free_p(bp) ;
  264.         return ;
  265.     }
  266.     pbp->cnt = 1 ;
  267.     *pbp->data = (PID_FIRST | PID_LAST | PID_NETROM) ;
  268.  
  269.     /* now format network header */
  270.     if ((hbp = htonnr3(&n3hdr)) == NULLBUF) {
  271.         free_p(pbp) ;
  272.         free_p(bp) ;
  273.         return ;
  274.     }
  275.  
  276.     append(&pbp,hbp) ;        /* append header to pid */
  277.     append(&pbp,bp) ;        /* append data to header */
  278.     send_ax25(axp,pbp) ;    /* pass it off to ax25 code */
  279. }
  280.     
  281.  
  282. /* Perform a nodes broadcast on interface # ifno in the net/rom
  283.  * interface table.
  284.  */
  285.  
  286. nr_bcnodes(ifno)
  287. unsigned ifno ;
  288. {
  289.     struct mbuf *hbp, *dbp, *savehdr ;
  290.     struct nrroute_tab *rp ;
  291.     struct nrnbr_tab *np ;
  292.     struct nr_bind * bp ;
  293.     struct nr3dest nrdest ;
  294.     int i, didsend = 0, numdest = 0 ;
  295.     register char *cp ;
  296.     struct interface *axif = nrifaces[ifno].interface ;
  297.     struct nr_bind *find_best() ;
  298.     
  299.     /* prepare the header */
  300.     if ((hbp = alloc_mbuf(NR3NODEHL)) == NULLBUF)
  301.         return ;
  302.         
  303.     hbp->cnt = NR3NODEHL ;    
  304.     
  305.     *hbp->data = NR3NODESIG ;
  306.     memcpy(hbp->data+1,nrifaces[ifno].alias,ALEN) ;
  307.  
  308.     /* Some people don't want to advertise any routes; they
  309.      * just want to be a terminal node.  In that case we just
  310.      * want to send our call and alias and be done with it.
  311.      */
  312.  
  313.     if (!nr_verbose) {
  314.         (*axif->output)(axif, (char *)&nr_nodebc, axif->hwaddr,
  315.                          (PID_FIRST | PID_LAST | PID_NETROM),
  316.                          hbp) ;    /* send it */
  317.         return ;
  318.     }
  319.     
  320.     /* make a copy of the header in case we need to send more than */
  321.     /* one packet */
  322.     savehdr = copy_p(hbp,NR3NODEHL) ;
  323.  
  324.     /* now scan through the routing table, finding the best routes */
  325.     /* and their neighbors.  create destination subpackets and append */
  326.     /* them to the header */
  327.     for (i = 0 ; i < NRNUMCHAINS ; i++) {
  328.         for (rp = nrroute_tab[i] ; rp != NULLNRRTAB ; rp = rp->next) {
  329.             /* look for best, non-obsolescent route */
  330.             if ((bp = find_best(rp->routes,0)) == NULLNRBIND)
  331.                 continue ;    /* no non-obsolescent routes found */
  332.             if (bp->quality == 0)    /* this is a loopback route */
  333.                 continue ;            /* we never broadcast these */
  334.             np = bp->via ;
  335.             /* insert best neighbor */
  336.             memcpy(nrdest.neighbor.call,np->call,ALEN) ;
  337.             nrdest.neighbor.ssid = np->call[ALEN] ;
  338.             /* insert destination from route table */
  339.             nrdest.dest = rp->call ;
  340.             /* insert alias from route table */
  341.             strcpy(nrdest.alias,rp->alias) ;
  342.             /* insert quality from binding */
  343.             nrdest.quality = bp->quality ;
  344.             /* create a network format destination subpacket */
  345.             if ((dbp = htonnrdest(&nrdest)) == NULLBUF) {
  346.                 free_p(hbp) ;    /* drop the whole idea ... */
  347.                 free_p(savehdr) ;
  348.                 return ;
  349.             }
  350.             append(&hbp,dbp) ;    /* append to header and others */
  351.             /* see if we have appended as many destinations */
  352.             /* as we can fit into a single broadcast.  If we */
  353.             /* have, go ahead and send them out. */
  354.             if (++numdest == NRDESTPERPACK) {    /* filled it up */
  355.                 didsend = 1 ;    /* indicate that we did broadcast */
  356.                 numdest = 0 ;    /* reset the destination counter */
  357.                 (*axif->output)(axif, (char *)&nr_nodebc, axif->hwaddr,
  358.                                  (PID_FIRST | PID_LAST | PID_NETROM),
  359.                                  hbp) ;    /* send it */
  360.                 hbp = copy_p(savehdr,NR3NODEHL) ;    /* new header */
  361.             }
  362.         }
  363.     }
  364.  
  365.     /* Now, here is something totally weird.  If our interfaces */
  366.     /* have different callsigns than this one, advertise a very */
  367.     /* high quality route to them.  Is this a good idea?  I don't */
  368.     /* know.  However, it allows us to simulate a bunch of net/roms */
  369.     /* hooked together with a diode matrix coupler. */
  370.     for (i = 0 ; i < nr_numiface ; i++) {
  371.         if (i == ifno)
  372.             continue ;        /* don't bother with ours */
  373.         cp = nrifaces[i].interface->hwaddr ;
  374.         if (!addreq((struct ax25_addr *)axif->hwaddr,(struct ax25_addr *)cp)) {
  375.             /* both destination and neighbor address */
  376.             memcpy((char *)&nrdest.dest,cp,AXALEN) ;
  377.             memcpy((char *)&nrdest.neighbor,cp,AXALEN) ;
  378.             /* alias of the interface */
  379.             strcpy(nrdest.alias,nrifaces[i].alias) ;
  380.             /* and the very highest quality */
  381.             nrdest.quality = 255 ;
  382.             /* create a network format destination subpacket */
  383.             if ((dbp = htonnrdest(&nrdest)) == NULLBUF) {
  384.                 free_p(hbp) ;    /* drop the whole idea ... */
  385.                 free_p(savehdr) ;
  386.                 return ;
  387.             }
  388.             append(&hbp,dbp) ;    /* append to header and others */
  389.             if (++numdest == NRDESTPERPACK) {    /* filled it up */
  390.                 didsend = 1 ;    /* indicate that we did broadcast */
  391.                 numdest = 0 ;    /* reset the destination counter */
  392.                 (*axif->output)(axif, (char *)&nr_nodebc, axif->hwaddr,
  393.                                  (PID_FIRST | PID_LAST | PID_NETROM),
  394.                                  hbp) ;    /* send it */
  395.                 hbp = copy_p(savehdr,NR3NODEHL) ;    /* new header */
  396.             }
  397.         }
  398.     }
  399.             
  400.     /* If we have a partly filled packet left over, or we never */
  401.     /* sent one at all, we broadcast: */
  402.     if (!didsend || numdest > 0)
  403.         (*axif->output)(axif, (char *)&nr_nodebc, axif->hwaddr,
  404.                         (PID_FIRST | PID_LAST | PID_NETROM), hbp) ;
  405.  
  406.     free_p(savehdr) ;    /* free the header copy */
  407. }
  408.  
  409.  
  410. /* initialize fake arp entry for netrom */
  411. nr3arp()
  412. {
  413.     int psax25(), setpath() ;
  414.  
  415.     arp_init(ARP_NETROM,AXALEN,0,0,NULLCHAR,psax25,setpath) ;
  416. }
  417.  
  418. /* attach the net/rom interface.  no parms for now. */
  419. /*ARGSUSED*/
  420. nr_attach(argc,argv)
  421. int argc ;
  422. char *argv[] ;
  423. {
  424.     if (nr_interface != (struct interface *)0) {
  425.         printf("netrom interface already attached\n") ;
  426.         return -1 ;
  427.     }
  428.  
  429.     nr3arp() ;
  430.     
  431.     nr_interface = (struct interface *)calloc(1,sizeof(struct interface)) ;
  432.     nr_interface->name = "netrom" ;
  433.     nr_interface->mtu = NR4MAXINFO ;
  434.     nr_interface->send = nr_send ;
  435.     nr_interface->next = ifaces ;
  436.     ifaces = nr_interface ;
  437.     return 0 ;
  438. }
  439.  
  440. /* This function checks an ax.25 address and interface number against
  441.  * the filter table and mode, and returns 1 if the address is to be
  442.  * accepted, and 0 if it is to be filtered out.
  443.  */
  444. static int
  445. accept_bc(addr,ifnum)
  446. struct ax25_addr *addr ;
  447. unsigned ifnum ;
  448. {
  449.     struct nrnf_tab *fp ;
  450.  
  451.     if (nr_nfmode == NRNF_NOFILTER)        /* no filtering in effect */
  452.         return 1 ;
  453.  
  454.     fp = find_nrnf(addr,ifnum) ;        /* look it up */
  455.     
  456.     if ((fp != NULLNRNFTAB && nr_nfmode == NRNF_ACCEPT)
  457.         || (fp == NULLNRNFTAB && nr_nfmode == NRNF_REJECT))
  458.         return 1 ;
  459.     else
  460.         return 0 ;
  461. }
  462.  
  463.  
  464. /* receive and process node broadcasts. */
  465. nr_nodercv(interface,source,bp)
  466. struct interface *interface ;
  467. struct ax25_addr *source ;
  468. struct mbuf *bp ;
  469. {
  470.     register int ifnum ;
  471.     char bcalias[7] ;
  472.     struct nr3dest ds ;
  473.     char sbuf[AXALEN*3] ;
  474.     
  475.     /* First, see if this is even a net/rom interface: */
  476.     for (ifnum = 0 ; ifnum < nr_numiface ; ifnum++)
  477.         if (interface == nrifaces[ifnum].interface)
  478.             break ;
  479.             
  480.     if (ifnum == nr_numiface) {    /* not in the interface table */
  481.         free_p(bp) ;
  482.         return ;
  483.     }
  484.  
  485.     if (!accept_bc(source,(unsigned)ifnum)) {    /* check against filter */
  486.         free_p(bp) ;
  487.         return ;
  488.     }
  489.     
  490.     /* See if it has a routing broadcast signature: */
  491.     if (uchar(pullchar(&bp)) != NR3NODESIG) {
  492.         free_p(bp) ;
  493.         return ;
  494.     }
  495.  
  496.     /* now try to get the alias */
  497.     if (pullup(&bp,bcalias,ALEN) < ALEN) {
  498.         free_p(bp) ;
  499.         return ;
  500.     }
  501.  
  502.     bcalias[ALEN] = '\0' ;        /* null terminate */
  503.  
  504.     /* copy source address and convert to arp format */
  505.     memcpy(sbuf,source->call,ALEN) ;
  506.     sbuf[ALEN] = (source->ssid | E) ;    /* terminate */
  507.     
  508.     /* enter the neighbor into our routing table */
  509.     if (nr_routeadd(bcalias,source,(unsigned)ifnum,nrifaces[ifnum].quality,
  510.                     sbuf, 0, 0) == -1) {
  511.         free_p(bp) ;
  512.         return ;
  513.     }
  514.     
  515.     /* we've digested the header; now digest the actual */
  516.     /* routing information */
  517.     while (ntohnrdest(&ds,&bp) != -1) {
  518.         /* ignore routes to me! */
  519.         if (ismycall(&ds.dest))
  520.             continue ;
  521.         /* ignore routes below the minimum quality threshhold */
  522.         if (ds.quality < nr_autofloor)
  523.             continue ;
  524.         /* set loopback paths to 0 quality */
  525.         if (ismycall(&ds.neighbor))
  526.             ds.quality = 0 ;
  527.         else
  528.             ds.quality = ((ds.quality * nrifaces[ifnum].quality + 128)
  529.                           / 256) & 0xff ;
  530.         if (nr_routeadd(ds.alias,&ds.dest,(unsigned)ifnum,ds.quality,sbuf,0,0)
  531.             == -1)
  532.             break ;
  533.     }
  534.             
  535.     free_p(bp) ;    /* This will free the mbuf if anything fails above */
  536. }
  537.  
  538.  
  539. /* The following are utilities for manipulating the routing table */
  540.  
  541. /* hash function for callsigns.  Look familiar? */
  542. int16
  543. nrhash(s)
  544. struct ax25_addr *s ;
  545. {
  546.     register char x ;
  547.     register int i ;
  548.     register char *cp ;
  549.  
  550.     x = 0 ;
  551.     cp = s->call ;
  552.     for (i = ALEN ; i !=0 ; i--)
  553.         x ^= *cp++ & 0xfe ;
  554.     x ^= s->ssid & SSID ;
  555.     return uchar(x) % NRNUMCHAINS ;
  556. }
  557.  
  558. /* Find a neighbor table entry.  Neighbors are determined by
  559.  * their callsign and the interface number.  This takes care
  560.  * of the case where the same switch or hosts uses the same
  561.  * callsign on two different channels.  This isn't done by
  562.  * net/rom, but it might be done by stations running *our*
  563.  * software.
  564.  */
  565. struct nrnbr_tab *
  566. find_nrnbr(addr,ifnum)
  567. register struct ax25_addr *addr ;
  568. unsigned ifnum ;
  569. {
  570.     int16 hashval ;
  571.     register struct nrnbr_tab *np ;
  572.     char i_state ;
  573.     struct ax25_addr ncall ;
  574.  
  575.     /* Find appropriate hash chain */
  576.     hashval = nrhash(addr) ;
  577.  
  578.     /* search hash chain */
  579.     i_state = disable() ;
  580.     for (np = nrnbr_tab[hashval] ; np != NULLNTAB ; np = np->next) {
  581.         memcpy(ncall.call,np->call,ALEN) ;    /* convert first in */
  582.         ncall.ssid = np->call[ALEN] ; /* list to ax25 address format */
  583.         if (addreq(&ncall,addr) && np->interface == ifnum) {
  584.             restore(i_state) ;
  585.             return np ;
  586.         }
  587.     }
  588.     restore(i_state) ;
  589.     return NULLNTAB ;
  590. }
  591.  
  592.  
  593. /* Find a route table entry */
  594. struct nrroute_tab *
  595. find_nrroute(addr)
  596. register struct ax25_addr *addr ;
  597. {
  598.     int16 hashval ;
  599.     register struct nrroute_tab *rp ;
  600.     char i_state ;
  601.  
  602.     /* Find appropriate hash chain */
  603.     hashval = nrhash(addr) ;
  604.  
  605.     /* search hash chain */
  606.     i_state = disable() ;
  607.     for (rp = nrroute_tab[hashval] ; rp != NULLNRRTAB ; rp = rp->next) {
  608.         if (addreq(&rp->call,addr)) {
  609.             restore(i_state) ;
  610.             return rp ;
  611.         }
  612.     }
  613.     restore(i_state) ;
  614.     return NULLNRRTAB ;
  615. }
  616.  
  617. /* Try to find the AX.25 address of a node with the given alias.  Return */
  618. /* a pointer to the AX.25 address if found, otherwise NULLAXADDR.  The alias */
  619. /* should be a six character, blank-padded, upper-case string. */
  620.  
  621. struct ax25_addr *
  622. find_nralias(alias)
  623. char *alias ;
  624. {
  625.     int i ;
  626.     register struct nrroute_tab *rp ;
  627.  
  628.     /* Since the route entries are hashed by ax.25 address, we'll */
  629.     /* have to search all the chains */
  630.     
  631.     for (i = 0 ; i < NRNUMCHAINS ; i++)
  632.         for (rp = nrroute_tab[i] ; rp != NULLNRRTAB ; rp = rp->next)
  633.             if (strncmp(alias, rp->alias, 6) == 0)
  634.                 return &rp->call ;
  635.  
  636.     /* If we get to here, we're out of luck */
  637.  
  638.     return NULLAXADDR ;
  639. }
  640.  
  641.     
  642. /* Find a binding in a list by its neighbor structure's address */
  643. struct nr_bind *
  644. find_binding(list,neighbor)
  645. struct nr_bind *list ;
  646. register struct nrnbr_tab *neighbor ;
  647. {
  648.     register struct nr_bind *bp ;
  649.  
  650.     for(bp = list ; bp != NULLNRBIND ; bp = bp->next)
  651.         if (bp->via == neighbor)
  652.             return bp ;
  653.  
  654.     return NULLNRBIND ;
  655. }
  656.  
  657. /* Find the worst quality non-permanent binding in a list */
  658. static
  659. struct nr_bind *
  660. find_worst(list)
  661. struct nr_bind *list ;
  662. {
  663.     register struct nr_bind *bp ;
  664.     struct nr_bind *worst = NULLNRBIND ;
  665.     unsigned minqual = 1000 ;     /* infinity */
  666.  
  667.     for (bp = list ; bp != NULLNRBIND ; bp = bp->next)
  668.         if (!(bp->flags & NRB_PERMANENT) && bp->quality < minqual) {
  669.             worst = bp ;
  670.             minqual = bp->quality ;
  671.         }
  672.  
  673.     return worst ;
  674. }
  675.  
  676. /* Find the best binding of any sort in a list.  If obso is 1,
  677.  * include entries below the obsolescence threshhold in the
  678.  * search (used when this is called for routing broadcasts).
  679.  * If it is 0, routes below the threshhold are treated as
  680.  * though they don't exist.
  681.  */
  682. static
  683. struct nr_bind *
  684. find_best(list,obso)
  685. struct nr_bind *list ;
  686. unsigned obso ;
  687. {
  688.     register struct nr_bind *bp ;
  689.     struct nr_bind *best = NULLNRBIND ;
  690.     int maxqual = -1 ;    /* negative infinity */
  691.  
  692.     for (bp = list ; bp != NULLNRBIND ; bp = bp->next)
  693.         if ((int)bp->quality > maxqual)
  694.             if (obso || bp->obsocnt >= obso_minbc) {
  695.                 best = bp ;
  696.                 maxqual = bp->quality ;
  697.             }
  698.  
  699.     return best ;
  700. }
  701.  
  702. /* Add a route to the net/rom routing table */
  703. nr_routeadd(alias,dest,ifnum,quality,neighbor,permanent,record)
  704. char *alias ;                /* net/rom node alias, blank-padded and */
  705.                             /* null-terminated */
  706. struct ax25_addr *dest ;    /* destination node callsign */
  707. unsigned ifnum ;            /* net/rom interface number */
  708. unsigned quality ;            /* route quality */
  709. char *neighbor ;            /* neighbor node + 2 digis (max) in arp format */
  710. unsigned permanent ;        /* 1 if route is permanent (hand-entered) */
  711. unsigned record ;            /* 1 if route is a "record route" */
  712. {
  713.     struct nrroute_tab *rp ;
  714.     struct nr_bind *bp ;
  715.     struct nrnbr_tab *np ;
  716.     int16 rhash, nhash ;
  717.     struct ax25_addr ncall ;
  718.  
  719.     /* See if a routing table entry exists for this destination */
  720.     if ((rp = find_nrroute(dest)) == NULLNRRTAB) {
  721.         if ((rp =
  722.              (struct nrroute_tab *)calloc(1,sizeof(struct nrroute_tab)))
  723.             == NULLNRRTAB)
  724.             return -1 ;
  725.         else {            /* create a new route table entry */
  726.             strncpy(rp->alias,alias,6) ;
  727.             rp->call = *dest ;
  728.             rhash = nrhash(dest) ;
  729.             rp->next = nrroute_tab[rhash] ;
  730.             if (rp->next != NULLNRRTAB)
  731.                 rp->next->prev = rp ;
  732.             nrroute_tab[rhash] = rp ;    /* link at head of hash chain */
  733.         }
  734.     } else if (!record) {
  735.         strncpy(rp->alias,alias,6) ;    /* update the alias */
  736.     }
  737.  
  738.     /* See if an entry exists for this neighbor */
  739.     memcpy(ncall.call,neighbor,ALEN) ;    /* no digis included */
  740.     ncall.ssid = neighbor[ALEN] ;
  741.     if ((np = find_nrnbr(&ncall,ifnum)) == NULLNTAB) {
  742.         if ((np =
  743.              (struct nrnbr_tab *)calloc(1,sizeof(struct nrnbr_tab)))
  744.              == NULLNTAB) {
  745.             if (rp->num_routes == 0) {    /* we just added to table */
  746.                 nrroute_tab[rhash] = rp->next ;
  747.                 free((char *)rp) ;                /* so get rid of it */
  748.             }
  749.             return -1 ;
  750.         }
  751.         else {        /* create a new neighbor entry */
  752.             memcpy(np->call,neighbor,3 * AXALEN) ;
  753.             np->interface = ifnum ;
  754.             nhash = nrhash(&ncall) ;
  755.             np->next = nrnbr_tab[nhash] ;
  756.             if (np->next != NULLNTAB)
  757.                 np->next->prev = np ;
  758.             nrnbr_tab[nhash] = np ;
  759.         }
  760.     }
  761.     else if (permanent) {        /* force this path to the neighbor */
  762.         memcpy(np->call,neighbor,3 * AXALEN) ;
  763.     }
  764.         
  765.     /* See if there is a binding between the dest and neighbor */
  766.     if ((bp = find_binding(rp->routes,np)) == NULLNRBIND) {
  767.         if ((bp =
  768.              (struct nr_bind *)calloc(1,sizeof(struct nr_bind)))
  769.             == NULLNRBIND) {
  770.             if (rp->num_routes == 0) {    /* we just added to table */
  771.                 nrroute_tab[rhash] = rp->next ;
  772.                 free((char *)rp) ;                /* so get rid of it */
  773.             }
  774.             if (np->refcnt == 0) {        /* we just added it */
  775.                 nrnbr_tab[nhash] = np->next ;
  776.                 free((char *)np) ;
  777.             }
  778.             return -1 ;
  779.         }
  780.         else {        /* create a new binding and link it in */
  781.             bp->via = np ;    /* goes via this neighbor */
  782.             bp->next = rp->routes ;    /* link into binding chain */
  783.             if (bp->next != NULLNRBIND)
  784.                 bp->next->prev = bp ;
  785.             rp->routes = bp ;
  786.             rp->num_routes++ ;    /* bump route count */
  787.             np->refcnt++ ;        /* bump neighbor ref count */
  788.             bp->quality = quality ;
  789.             bp->obsocnt = obso_init ;    /* use initial value */
  790.             if (permanent)
  791.                 bp->flags |= NRB_PERMANENT ;
  792.             else if (record)    /* notice permanent overrides record! */
  793.                 bp->flags |= NRB_RECORDED ;
  794.         }
  795.     } else {
  796.         if (permanent) {    /* permanent request trumps all */
  797.             bp->quality = quality ;
  798.             bp->obsocnt = obso_init ;
  799.             bp->flags |= NRB_PERMANENT ;
  800.             bp->flags &= ~NRB_RECORDED ;    /* perm is not recorded */
  801.         } else if (!(bp->flags & NRB_PERMANENT)) {    /* not permanent */
  802.             if (record) {    /* came from nr_route */
  803.                 if (bp->flags & NRB_RECORDED) { /* no mod non-rec bindings */
  804.                     bp->quality = quality ;
  805.                     bp->obsocnt = obso_init ; /* freshen recorded routes */
  806.                 }
  807.             } else {        /* came from a routing broadcast */
  808.                 bp->quality = quality ;
  809.                 bp->obsocnt = obso_init ;
  810.                 bp->flags &= ~NRB_RECORDED ; /* no longer a recorded route */
  811.             }
  812.         }
  813.     }
  814.         
  815.     /* Now, check to see if we have too many bindings, and drop */
  816.     /* the worst if we do */
  817.     if (rp->num_routes > nr_maxroutes) {
  818.         /* since find_worst never returns permanent entries, the */
  819.         /* limitation on number of routes is circumvented for    */
  820.         /* permanent routes */
  821.         if ((bp = find_worst(rp->routes)) != NULLNRBIND) {
  822.             memcpy(ncall.call,bp->via->call,ALEN) ;
  823.             ncall.ssid = bp->via->call[ALEN] ;
  824.             nr_routedrop(dest,&ncall,bp->via->interface) ;
  825.         }
  826.     }
  827.  
  828.     return 0 ;
  829. }
  830.  
  831.  
  832. /* Drop a route to dest via neighbor */
  833. nr_routedrop(dest,neighbor,ifnum)
  834. struct ax25_addr *dest, *neighbor ;
  835. unsigned ifnum ;
  836. {
  837.     register struct nrroute_tab *rp ;
  838.     register struct nrnbr_tab *np ;
  839.     register struct nr_bind *bp ;
  840.  
  841.     if ((rp = find_nrroute(dest)) == NULLNRRTAB)
  842.         return -1 ;
  843.  
  844.     if ((np = find_nrnbr(neighbor,ifnum)) == NULLNTAB)
  845.         return -1 ;
  846.  
  847.     if ((bp = find_binding(rp->routes,np)) == NULLNRBIND)
  848.         return -1 ;
  849.  
  850.     /* drop the binding first */
  851.     if (bp->next != NULLNRBIND)
  852.         bp->next->prev = bp->prev ;
  853.     if (bp->prev != NULLNRBIND)
  854.         bp->prev->next = bp->next ;
  855.     else
  856.         rp->routes = bp->next ;
  857.  
  858.     free((char *)bp) ;
  859.     rp->num_routes-- ;        /* decrement the number of bindings */
  860.     np->refcnt-- ;            /* and the number of neighbor references */
  861.     
  862.     /* now see if we should drop the route table entry */
  863.     if (rp->num_routes == 0) {
  864.         if (rp->next != NULLNRRTAB)
  865.             rp->next->prev = rp->prev ;
  866.         if (rp->prev != NULLNRRTAB)
  867.             rp->prev->next = rp->next ;
  868.         else
  869.             nrroute_tab[nrhash(dest)] = rp->next ;
  870.  
  871.         free((char *)rp) ;
  872.     }
  873.  
  874.     /* and check to see if this neighbor can be dropped */
  875.     if (np->refcnt == 0) {
  876.         if (np->next != NULLNTAB)
  877.             np->next->prev = np->prev ;
  878.         if (np->prev != NULLNTAB)
  879.             np->prev->next = np->next ;
  880.         else
  881.             nrnbr_tab[nrhash(neighbor)] = np->next ;
  882.  
  883.         free((char *)np) ;
  884.     }
  885.     
  886.     return 0 ;
  887. }
  888.  
  889. /* Find the best neighbor for destination dest, in arp format */
  890. char *
  891. nr_getroute(dest)
  892. struct ax25_addr *dest ;
  893. {
  894.     register struct nrroute_tab *rp ;
  895.     register struct nr_bind *bp ;
  896.  
  897.     if ((rp = find_nrroute(dest)) == NULLNRRTAB)
  898.         return NULLCHAR ;
  899.  
  900.     if ((bp = find_best(rp->routes,0)) == NULLNRBIND)    /* shouldn't happen! */
  901.         return NULLCHAR ;
  902.  
  903.     return bp->via->call ;
  904. }
  905.  
  906. /* Find an entry in the filter table */
  907. struct nrnf_tab *
  908. find_nrnf(addr,ifnum)
  909. register struct ax25_addr *addr ;
  910. unsigned ifnum ;
  911. {
  912.     int16 hashval ;
  913.     register struct nrnf_tab *fp ;
  914.  
  915.     /* Find appropriate hash chain */
  916.     hashval = nrhash(addr) ;
  917.  
  918.     /* search hash chain */
  919.     for (fp = nrnf_tab[hashval] ; fp != NULLNRNFTAB ; fp = fp->next) {
  920.         if (addreq(&fp->neighbor,addr) && fp->interface == ifnum) {
  921.             return fp ;
  922.         }
  923.     }
  924.  
  925.     return NULLNRNFTAB ;
  926. }
  927.  
  928. /* Add an entry to the filter table.  Return 0 on success,
  929.  * -1 on failure
  930.  */
  931. int
  932. nr_nfadd(addr,ifnum)
  933. struct ax25_addr *addr ;
  934. unsigned ifnum ;
  935. {
  936.     struct nrnf_tab *fp ;
  937.     int16 hashval ;
  938.     
  939.     if (find_nrnf(addr,ifnum) != NULLNRNFTAB)
  940.         return 0 ;    /* already there; it's a no-op */
  941.  
  942.     if ((fp = (struct nrnf_tab *)calloc(1,sizeof(struct nrnf_tab)))
  943.         == NULLNRNFTAB)
  944.         return -1 ;    /* no storage */
  945.  
  946.     hashval = nrhash(addr) ;
  947.     fp->neighbor = *addr ;
  948.     fp->interface = ifnum ;
  949.     fp->next = nrnf_tab[hashval] ;
  950.     if (fp->next != NULLNRNFTAB)
  951.         fp->next->prev = fp ;
  952.     nrnf_tab[hashval] = fp ;
  953.  
  954.     return 0 ;
  955. }
  956.  
  957. /* Drop a neighbor from the filter table.  Returns 0 on success, -1
  958.  * on failure.
  959.  */
  960. int
  961. nr_nfdrop(addr,ifnum)
  962. struct ax25_addr *addr ;
  963. unsigned ifnum ;
  964. {
  965.     struct nrnf_tab *fp ;
  966.  
  967.     if ((fp = find_nrnf(addr,ifnum)) == NULLNRNFTAB)
  968.         return -1 ;    /* not in the table */
  969.  
  970.     if (fp->next != NULLNRNFTAB)
  971.         fp->next->prev = fp->prev ;
  972.     if (fp->prev != NULLNRNFTAB)
  973.         fp->prev->next = fp->next ;
  974.     else
  975.         nrnf_tab[nrhash(addr)] = fp->next ;
  976.  
  977.     free((char *)fp) ;
  978.  
  979.     return 0 ;
  980. }
  981.